package com.md.application.assembler;

import com.md.base.exception.ServerException;
import com.md.request.RegisterSystemUserRequest;
import com.md.util.MatchUtil;
import com.md.util.ObjectUtil;
import com.md.value.enums.ErrorEnum;
import lombok.experimental.UtilityClass;

/**
 * @author 刘骄阳
 * 2024-12-15 1:49
 */
@UtilityClass
public class SystemUsersAssembler {
  public static void checkRegisterUserRequest(final RegisterSystemUserRequest request) {
    if (ObjectUtil.isEmpty(request.getRegisterType())) {
      throw ServerException.build(ErrorEnum.IS_EMPTY, "注册类型");
    }

    switch (request.getRegisterType()) {
      case EMAIL:
        break;
      case PHONE:
        if (ObjectUtil.isEmpty(request.getPhone())) {
          throw ServerException.build(ErrorEnum.IS_EMPTY, "手机号");
        }
        if (!MatchUtil.isPhone(request.getPhone())) {
          throw ServerException.build(com.md.infrastructure.value.ErrorEnum.REGISTER_INFO_ILLEGAL, "手机号");
        }
        if (ObjectUtil.isEmpty(request.getCode())) {
          throw ServerException.build(ErrorEnum.IS_EMPTY, "验证码");
        }
        break;
      default:
        throw new IllegalStateException("Unexpected value: " + request.getRegisterType());
    }
    if (ObjectUtil.isEmpty(request.getPassword())) {
      throw ServerException.build(ErrorEnum.IS_EMPTY, "密码");
    }
  }
}
